package com.hhd.pdf.component;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SectionComponent3 extends AbstractPdfComponent {

    @Override
    public List<Element> doGenElements(GeneratorContext context) {
        Image image = PdfUtils.getImage("SectionBar.png");
        PdfPTable table = PdfUtils.createTable(new float[]{50});
        resolveDefaultPadding(context);
//        addLeftCell(image, table);
        addRightCell(image, table, context);
        return CommonUtils.newList(table);
    }

    private void resolveDefaultPadding(GeneratorContext context) {
        context.getParsingElement().addAttribute("paddingBottom", "5");
        context.getParsingElement().addAttribute("paddingLeft", "25");
    }

    private void addRightCell(Image image, PdfPTable table, GeneratorContext context) {
        PdfPCell cell = PdfUtils.createCell();
        cell.setVerticalAlignment(PdfPCell.ALIGN_LEFT);
        cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        GeneratorContext runtime = context.runtime(context.getParsingElement());
        runtime.getGeneratorParameters().setColor(PdfUtils.createColor(48, 111, 220));
        List<Element> elements = genChildren(runtime, context.getParsingElement());
        if (!CommonUtils.isEmpty(elements)) {
            elements.forEach(cell::addElement);
        }
        table.addCell(cell);
    }

    private void addLeftCell(Image image, PdfPTable table) {
        PdfPCell cell = PdfUtils.createCell(image);
        cell.setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
        table.addCell(cell);
    }

    @Override
    public String getName() {
        return "section3";
    }
}
